package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.contract.RecordContract;
import com.rdc.qingkou.presenter.RecordPresenter;
import com.rdc.qingkou.widget.RecordButton;



public class RecordDialogActivity extends BaseActivity<RecordPresenter> implements RecordContract.View, View.OnClickListener {

    private RecordButton mRecord;
    private Button mBack;
    private Button mConfirm;
    private String mAudioFile = "";



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.send_record);
        // StatusBarCompat.setStatusBarColor(this,getResources().getColor(R.color.windowBackground),true);
        init();
    }

    private void init() {
        mRecord = (RecordButton) findViewById(R.id.btn_record);
        mBack = (Button) findViewById(R.id.btn_back);
        mConfirm = (Button) findViewById(R.id.btn_confirm);
        mBack.setOnClickListener(this);
        mConfirm.setOnClickListener(this);
        mRecord.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                if (TextUtils.isEmpty(mAudioFile)){
                    mPresenter.stop();
                    Intent intent = new Intent();
                    intent.putExtra("AudioFile", mAudioFile);
                    setResult(RESULT_OK, intent);
                    finish();
                }else {
                    mPresenter.cancel();
                    mAudioFile = "";
                    mRecord.setEnabled(true);
                }

                break;
            case R.id.btn_confirm:
                mPresenter.stop();
                Intent intent = new Intent();
                intent.putExtra("AudioFile", mAudioFile);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.btn_record:
                mAudioFile = mPresenter.rerord();
                mRecord.setEnabled(false);
                break;
        }
    }

    @Override
    public void setProgressBar(int time) {
        mRecord.setPercent(time);
    }

    @Override
    public void setRecord(String string) {
        mRecord.setText(string);
    }

    @Override
    public RecordPresenter getInstance() {
        return new RecordPresenter();
    }


    @Override
    public void onBackPressed() {

        if (TextUtils.isEmpty(mAudioFile)){
            mPresenter.stop();
            Intent intent = new Intent();
            intent.putExtra("AudioFile", mAudioFile);
            setResult(RESULT_OK, intent);
            finish();
        }else {
            mPresenter.cancel();
            mAudioFile = "";
            mRecord.setEnabled(true);
            finish();
        }

    }
}
